package io.kiki.stack.netty;

import io.kiki.stack.netty.config.*;
import io.kiki.stack.netty.config.configs.ConfigContainer;
import io.kiki.stack.netty.config.configs.DefaultConfigContainer;
import io.kiki.stack.netty.config.switches.GlobalSwitch;
import io.kiki.stack.netty.lifeCycle.AbstractLifeCycle;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter
@Getter
public abstract class AbstractClient extends AbstractLifeCycle implements IClient, Configurable {

    private final Options options;
    private final GlobalSwitch globalSwitch;
    private final ConfigContainer configContainer;

    public AbstractClient() {
        this.options = new Options();
        this.globalSwitch = new GlobalSwitch();
        if (ConfigManager.conn_reconnect_switch()) {
            option(ClientOption.Channel_reconnect_switch, true);
        } else {
            option(ClientOption.Channel_reconnect_switch, false);
        }

        if (ConfigManager.conn_monitor_switch()) {
            option(ClientOption.Channel_monitor_switch, true);
        } else {
            option(ClientOption.Channel_monitor_switch, false);
        }
        this.configContainer = new DefaultConfigContainer();
    }

    @Override
    public <T> T option(Option<T> option) {
        return options.option(option);
    }

    @Override
    public <T> Config option(Option<T> option, T value) {
        options.option(option, value);
        return this;
    }

    @Override
    @Deprecated
    public ConfigContainer conf() {
        return this.configContainer;
    }

    @Override
    @Deprecated
    public GlobalSwitch switches() {
        return this.globalSwitch;
    }

    @Override
    public void initWriteBufferWaterMark(int low, int high) {
        option(ClientOption.netty_buffer_low_water_mark, low);
        option(ClientOption.netty_buffer_high_water_mark, high);
    }

    @Override
    public int netty_buffer_low_watermark() {
        return option(ClientOption.netty_buffer_low_water_mark);
    }

    @Override
    public int netty_buffer_high_watermark() {
        return option(ClientOption.netty_buffer_high_water_mark);
    }
}
